package project.dade.cn.bst.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.afollestad.materialdialogs.Theme;
import com.bumptech.glide.Glide;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.squareup.okhttp.Request;

import java.util.HashMap;

import butterknife.InjectView;
import project.dade.cn.bst.R;
import project.dade.cn.bst.base.ZJPBaseBarActivity;
import project.dade.cn.bst.manager.ApiConfig;
import project.dade.cn.bst.manager.AppApplication;
import project.dade.cn.bst.network.FormatRequest;
import project.dade.cn.bst.network.callback.ResultCallback;
import project.dade.cn.bst.network.request.OkHttpRequest;
import project.dade.cn.bst.utils.LogUtil;
import project.dade.cn.bst.utils.NetRequestUtil;
import project.dade.cn.bst.utils.ToastUtil;

/**
 * Created by vain zhou on 2016/4/28.
 */
public class RefundActivity extends ZJPBaseBarActivity implements View.OnClickListener{
    private String orderno;
    private  String orderimage;
    private String price;
    private String title;
    private String id;
    private String quantity;
    @InjectView(R.id.title)
    TextView tv_title;
    @InjectView(R.id.sell)
    TextView sell;
    @InjectView(R.id.photo)
    ImageView imageView;
    @InjectView(R.id.layout_type)
    RelativeLayout type;
    @InjectView(R.id.tv_submit_tui)
    Button tui;
     public int num=0;
    private KProgressHUD hud;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_refund);
        orderno=getIntent().getStringExtra("orderno");
        orderimage=getIntent().getStringExtra("orderimage");
        price=getIntent().getStringExtra("price");
        title=getIntent().getStringExtra("title");
        id=getIntent().getStringExtra("id");
        quantity=getIntent().getStringExtra("num");
        tv_title.setText(title);
        sell.setText(price);
        Glide.with(this).load(orderimage).placeholder(R.drawable.image_loader_small).error(R.drawable.image_loader_small).into(imageView);
        type.setOnClickListener(this);
        tui.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.layout_type:
                new MaterialDialog.Builder(this)
                        .title("选择规格")
                        .titleColor(getResources().getColor(R.color.background_material_dark))
                        .theme(Theme.LIGHT)
                        .items(R.array.tuikuan)
                        .itemsCallback(new MaterialDialog.ListCallback() {
                            @Override
                            public void onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                                num=which;

                            }
                        })
                        .positiveText(android.R.string.cancel)
                        .show();
                break;

            case R.id.tv_submit_tui:
                requestTuiorder();
                break;
        }

    }
    private void requestTuiorder() {
        HashMap<String, String> params = new HashMap<String, String>();
        hud = KProgressHUD.create(RefundActivity.this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setLabel("正在加载")
                .setCancellable(true);
        hud.show();
        params.put("uid", AppApplication.getInstance().getSpUtil().getUserID().toString());
        params.put("mobilephone", AppApplication.getInstance().getSpUtil().getUSER_mobile());
        params.put("orderno",orderno);
        params.put("proid",id);
        params.put("pronum",quantity);
        params.put("applytype",num+"");
        params.put("reason","七天无理由退款");
        params.put("Sign", FormatRequest.main(params));
        new OkHttpRequest.Builder()
                .url(ApiConfig.getHostAddress() + ApiConfig.SERVER_URL.RefundApply).params(params)
                .post(new ResultCallback<String>() {
                    @Override
                    public void onError(Request request, Exception e) {
                          hud.dismiss();
                        ToastUtil.showShort(RefundActivity.this, "网络相应失败请稍后再试");
                    }

                    @Override
                    public void onResponse(String response) {

                        LogUtil.i(TAG, "退款信息：" + response);
                        if (TextUtils.isEmpty(NetRequestUtil.requestsError(response, "获取订单列表"))) {
                            hud.dismiss();
                            Intent mIntent = new Intent(RefundActivity.this,
                                    MyorderActivity.class);
                            startActivity(mIntent);
                            finish();
                        }else {
                             hud.dismiss();
                            ToastUtil.showShort(RefundActivity.this, NetRequestUtil.requestsError(response, ""));


                        }
                    }

                });


    }
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            this.finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
